<?php
/**
 * controler de traduction
 *
 * @author Gmax
 */
class controler_translate extends controler{
    private $_target = null;
    private $_translate = null;
    private $_lang = '';
    
    function __construct(){}
    
    /**
     * initialisation du traducteur
     * @param \modele $target
     * @param string $lang
     */
    public function init($target,$lang=''){
        $this->setTarget($target);
        $this->setLangue($lang);
        $this->setTranslatedTarget($target);
    }
    
    /**
     * renvoi la liste des proprietes traduites de l'objet
     * @param boolean $input
     * @param string $lang
     * @return array
     */
    public function getDatas($input=false,$lang=''){
        $datas = $this->getTarget()->getDatas($input);
        $targetLang = ($lang!=='')?$lang:$this->getLangue();
        if($targetLang!==''&&$this->getTranslatedTarget()!==null){
            $traductionTable = $this->getTranslatedTarget()->getTraduction($targetLang,$input);
            $translatedDatas = (isset($traductionTable[$targetLang]))?$traductionTable[$targetLang]:array();
            foreach ($translatedDatas as $k=>$v){
                $datas[$k] = $v;
            }
        }
        return $datas;
    }
    
    /**
     * defini la target courante
     * @param \modele $target
     */
    public function setTarget($target){
        $this->_target = $target;
    }
    /**
     * envoi la target courante
     * @return \modele
     */
    public function getTarget(){
        return $this->_target;
    }
    /**
     * renvoi l'identifiant du modele target
     * @return int
     */
    public function getTargetId(){
        return ($this->getTarget()!==null)?$this->getTarget()->getId():0;
    }
    /**
     * renvoi le type du modele target
     * @return string
     */
    public function getTargetType(){
        return ($this->getTarget()!==null)?$this->getTarget()->type:'';
    }
    /**
     * défini la langue courante
     * @param string $lang
     */
    public function setLangue($lang=''){
        $this->_lang = $lang;
    }
    /**
     * renvoi la langue courante
     * @return string
     */
    public function getLangue(){
        return $this->_lang;
    }
    
    /**
     * defini l'objet de rtaduction
     * @param \modele $target
     */
    public function setTranslatedTarget($target){
        if($this->getTarget()===null || $this->getTarget()!==$target){
            $this->setTarget($target);
        }
        $this->_translate = new modele_translate($this->getTargetId(), array('type'=>'object','modele'=>$this->getTargetType(),'lang'=>$this->getLangue()));
        
    }
    /**
     * renvoi l'objet de traduction
     * @return \modele_translate
     */
    public function getTranslatedTarget(){
        return $this->_translate;
    }
    
    // methodes static 
    
    /**
    * renvoi un controler de traduction
    * @param \modele $target
    * @param string $lang
    * @return \controler_translate
    */
    public static function getTranslator($target,$lang=''){
        $translator = new controler_translate();
        $translator->init($target,$lang);
        return $translator;
    }
    /**
     * renvoi la valeur traduite de la propriete, la valeur d'origine est renvoyée si aucune traduction n'est trouvée
     * @param \modele $object
     * @param string $lang
     * @param string $property
     * @param boolean $input
     * @return string
     */
    public static function doTranslate($object,$lang,$property,$input=false){
        $translate = ($lang!==''&&$lang!=='fr')?modele_translate::findObjectProperty($object->getId(),$object->type, $property, $lang):null;
        if($translate!==null){
            $retour = $translate->getContent($input);
        }else{
            $datas = $object->getDatas($input);
            $retour = (isset($datas[$property]))?$datas[$property]:'';
            if($retour==='' && substr($property, 0, 11)==='custom_vars'){
                $retour = (isset($datas['custom_vars'])&&isset($datas['custom_vars'][substr($property, 12,-1)]))?$datas['custom_vars'][substr($property, 12,-1)]:'';
            }
        }
        
        return $retour;
    }
}
?>